<template>
  <div class="goods">
    <div class="wrap">
      <Crumb :goods="good"> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="{ active: index == options1Active }"
            v-for="(item, index) in options1"
            :key="index"
            @click="options1Fn(index, item.min, item.max)"
          >
            {{ item.txt }}</span
          >

          <!-- <span>我还可以兑换</span>
          <span>0-500个</span>
          <span>500-1000个</span>
          <span>1000-1500个</span>
          <span>1500-2500个</span> -->
        </li>
        <li>
          <strong>分类：</strong>
          <span
            :class="{ active: item.isActive }"
            v-for="(item, index) in options2"
            :key="index"
            @click="options2Fn(index, item.type)"
            >{{ item.txt }}</span
          >
          <!-- <span>实物礼品</span>
          <span>虚拟礼品</span> -->
        </li>
      </ul>
      <List :arr="goodsListShow" />
      <div style="text-align: center; margin-top: 20px">
        <i class="dong iconfont icon-loading"></i>
        {{ isReachBottom ? "已经没有数据了" : "正在加载中..." }}
      </div>
    </div>
  </div>
</template>                            
<script>
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
//导入商品搜索 api
import { GoodsSearchApi } from "@/request/api";
//导入utils
import { getScrollTop, getClientHeight, getScrollHeight } from "@/utils";
export default {
  data() {
    return {
      good: [
        { name: "首页", link: "#" },
        { name: "全部商品", link: "#" },
      ],
      //栏目 0全部 1精品 2 热门
      did: 0,
      //类型 0 全部 1实体 2虚拟
      type: 0,
      //最小积分
      min: 0,
      //最大积分
      max: 0,
      //搜索关键字
      keyword: "",
      // 商品列表数组
      goodsList: [],

      //默认显示第一页
      goodsListShow: [],

      // 排序的数组项
      options1: [
        { min: 0, max: 10000, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      options1Active: 0,
      options2: [
        { txt: "全部", type: 0, isActive: true },
        { txt: "实物礼品", type: 1, isActive: false },
        { txt: "虚拟礼品", type: 2, isActive: false },
      ],
      //展示第一页
      page: 1,
      //默认8条
      size: 8,
      // 是否显示数据
      isLoading: false,
      //是否已经到低
      isReachBottom: false,
    };
  },
  components: {
    Crumb,
    List,
  },
  created() {
    //对应的商品列表请求还缺少关键字
    this.keyword = this.$route.query.keyword;
    this.goodsSearch();
  },

  mounted() {
    //监听滚动事件
    window.addEventListener("scroll", this.scrollFn);
  },
  beforeDestroy() {
    //销毁,不让其他页面也有滚动的效果
    window.removeEventListener("scroll", this.scrollFn);
  },
  watch: {
    //路由监听，刷新当前页
    "$route.query.keyword": {
      handler(newVal, oldVal) {
        this.keyword = newVal;
        this.goodsSearch();
      },
    },
  },
  methods: {
    async goodsSearch() {
      let res = await GoodsSearchApi({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      });
      // console.log("全部商品", res);
      this.goodsList = res.data;

      //筛选一页8条数据
      this.goodsListShow = this.goodsList.filter((item, index) => index < 8);

      //初始化
      this.page = 1;
      this.isReachBottom = false;
      //数据是否到达底部
      if (this.goodsListShow.length == this.goodsList.length) {
        this.isReachBottom = true;
        return;
      }
    },
    options1Fn(idx, min, max) {
      this.options1Active = idx;
      console.log(min, max);
      //更新查询条件
      this.min = min;
      this.max = max;
      // 重新查询
      this.goodsSearch();
    },

    options2Fn(idx, type) {
      //排他
      //让所有属性都为false
      this.options2.forEach((item) => (item.isActive = false));
      // 让当前自己为true
      this.options2[idx].isActive = true;

      //筛选
      this.type = type;
      //重新查询
      this.goodsSearch();
      console.log("type", type);
    },
    scrollFn() {
      // console.log(
      //   "滚动了",
      //   getScrollTop() + getClientHeight(),
      //   getScrollHeight()
      // );
      // 当可视高度+滚动高度 > 文档高度时 准备加载下一页数据
      if (getScrollTop() + getClientHeight() >= getScrollHeight() - 300) {
        //判断数据是否加载完毕
        if (this.goodsListShow.length == this.goodsList.length) {
          this.isReachBottom = true;
          return;
        }

        // 当滑动过快时, 会频繁执行scrollFn,触发this.page++, 一下子加载了多页数据
        if (this.isLoading == false) {
          this.isLoading = true;
          this.page++;
          console.log("可以加载下一页数据了");
          // 前提: 页面已经显示了第一页 每页显示8条数据
          // 方式一: 调用后台接口 传递第二页,8条数据参数过去  后台返回结果
          //fangshier: 往goodsList 数组中拿第二页的数据,把数据从goodsList push到 goodsListShow
          //  page:1 size:8  index:0~7 数据 created()
          //  page:2 size:8  index:8~15 数据 created()
          //  page:3 size:8  index:16~23 数据 created()

          //使用定时器模拟加载速度的时间
          setTimeout(() => {
            for (
              let i = (this.page - 1) * this.size;
              i < this.page * this.size;
              i++
            ) {
              this.goodsList[i] && this.goodsListShow.push(this.goodsList[i]);
            }
            this.isLoading = false;
          }, 1000);
        }
      }
    },
  },
};
</script>
 
<style lang = "less" scoped>
@import "../css/css.less";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
   i{
        animation: dong 3s infinite ease-out;
      }
      @keyframes dong{
        100% {
          transform: rotate(0deg);
        }
      }
}
</style>